﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;
using SCM.Common.Extensions;
using SCM.Model;
using SCM.Servie.IBusiness;

namespace SCM.Manage.Controllers
{
    [Authorize]
    public class SysUserController : BaseController
    {
        public IActionResult Index()
        {
            return View();
        }

        #region 添加
        public ActionResult AddView()
        {
            BindRole();
            return View();
        }

        public IActionResult Add(AddUpdateUserVo entity)
        {
            #region 数据验证
            if (string.IsNullOrWhiteSpace(entity.UserAccount))
            {
                return Error("帐号不能为空");
            }

            if (string.IsNullOrWhiteSpace(entity.UserDisplayName))
            {
                return Error("姓名不能为空");
            }

            if (string.IsNullOrWhiteSpace(entity.UserPassword))
            {
                return Error("密码不能为空");
            }

            if (string.IsNullOrWhiteSpace(entity.RoleID))
            {
                return Error("请选择角色");
            }
            #endregion

            entity.ID = Guid.NewGuid().ToString();
            entity.CompanyID = CurrentCompanyID;
            entity.Creator = CurrentUserId;
            entity.CreatorName = CurrentUserName;
            entity.UserType = UserTypeEnum.General;
            entity.UserPassword = EncryptHelper.HashMD5String(entity.UserPassword, Encoding.UTF8);

            var service = GetService<IUserService>();
            bool result = service.Add(entity) ;
            return result ? GetOk(result) : Error("保存失败");
        }
        #endregion

        #region 查询
        [HttpPost]
        public IActionResult Search(string userAccount, string userDisplayName, int index = 1)
        {
            var service = GetService<IUserService>();
            var userPage = service.Query(CurrentCompanyID, userAccount, userDisplayName, new Common.PageInfo() { PageIndex = index, PageSize = 10 });
            return GetOk(new { totalCount = userPage.Page.DataCount, items = userPage.Data });
        }

        #endregion

        #region 编辑
        public ActionResult EditView(string id)
        {
            BindRole();
            var model = GetService<IUserService>().GetById(id);
            return View(model);
        }

        public IActionResult Modify(AddUpdateUserVo entity)
        {
            entity.UserPassword = EncryptHelper.HashMD5String(entity.UserPassword, Encoding.UTF8);
            entity.ModifyUserId = CurrentUserId;
            entity.ModifyUserName = CurrentUserName;

            bool result = GetService<IUserService>().Update(entity);
            return result ? GetOk(result) : Error("保存失败");
        }
        #endregion

        #region 删除
        /// <summary>
        /// 删除  【支持批量操作】
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public IActionResult Delete(string ids)
        {
            if (string.IsNullOrWhiteSpace(ids) || ids.Split(',').Length < 0)
            {
                return Error("请选择数据");
            }
            var delIds = ids.Split(',').ToList();
            bool result = GetService<IUserService>().Delete(delIds);
            return result ? GetOk(result) : Error("删除失败");
        }
        #endregion

        #region 数据绑定
        private void BindRole()
        {
            List<SelectListItem> roleList = new List<SelectListItem>();
            roleList.Add(new SelectListItem() { Text = "请选择", Value = "", Selected = true });

            var roleService = GetService<IRoleService>();
            var roles = roleService.GetList(CurrentCompanyID);
            roles.ForEach(item =>
            {
                roleList.Add(new SelectListItem() { Text = item.RoleDescription, Value = item.ID });
            });

            ViewBag.RoleList = roleList;
        }
        #endregion
    }
}
